#! /bin/bash

DIST_FILE_NAME='app.war'
BASE_DIR=`pwd`
DIST_DIR=$BASE_DIR'/dist'
DIST_FILE_PATH=$DIST_DIR/$DIST_FILE_NAME
DEPLOY_PATH=$BASE_DIR/app/webapps/ROOT

APP_CONFIG_PATH=$BASE_DIR/app/webapps/ROOT/WEB-INF/classes
APP_FILE_PATH=$APP_CONFIG_PATH/db.properties

DB_CONFIG_PATH=$DIST_DIR/db.properties

if [ -e $DIST_FILE_PATH  ]; then
    if [ ! -d $DEPLOY_PATH ]; then
        mkdir -p $DEPLOY_PATH
    fi

    cd $DEPLOY_PATH
    
    rm -rf ./* && \
        cp $DIST_FILE_PATH $DEPLOY_PATH
    
    unzip $DIST_FILE_NAME
    rm -rf $DIST_FILE_NAME

    if [ -e $DB_CONFIG_PATH ]; then
        rm -rf $APP_FILE_PATH && \
            cp $DB_CONFIG_PATH $APP_CONFIG_PATH
    fi
    if [ -e $DIST_DIR/config.properties ]; then
        rm -rf $APP_CONFIG_PATH/config.properties && \
            cp $DIST_DIR/config.properties $APP_CONFIG_PATH/config.properties
    fi

else
    echo "cant find $DIST_FILE_PATH"
fi
